package com.zhangh.summer.admin.support.health;

import com.zhangh.summer.admin.support.ds.common.setting.DsSettingProperties;
import com.zhangh.summer.admin.support.multipleds.DataSourceContextHolder;
import com.zhangh.summer.admin.support.multipleds.IDynamicDataSource;
import com.zhangh.summer.admin.support.multipleds.MultiDsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;

import javax.sql.DataSource;

@Slf4j
public class DataSourceHealth extends DataSourceHealthIndicator implements InitializingBean {
    @Autowired
    private DataSource mySource;
    @Autowired
    private IDynamicDataSource dynamicDataSource;
    @Autowired
    private DsSettingProperties dsSettingProperties;

    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        log.info(">>>doHealthCheck");
        DataSourceContextHolder.setDataSourceType(MultiDsUtil.getDsType(dsSettingProperties.getDefaultDsName()));
        setDataSource(mySource);
        super.doHealthCheck(builder);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        //log.info(">>>health.afterPropertiesSet");
    }
}
